package com.ruoyi.datastatistics.service.impl;

import com.ruoyi.basedata.domain.vo.TerminalsDbProtocol;
import com.ruoyi.datastatistics.domain.Realtimegatherdata;
import com.ruoyi.datastatistics.domain.TerminalHistoryData;
import com.ruoyi.datastatistics.mapper.RealtimegatherdataMapper;
import com.ruoyi.datastatistics.service.IRealtimegatherdataService;
import com.ruoyi.datastatistics.wxentity.MapTerminalInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 实时数据采集（大）Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@Service
public class RealtimegatherdataServiceImpl implements IRealtimegatherdataService {
    @Autowired
    private RealtimegatherdataMapper realtimegatherdataMapper;

    /**
     * 查询实时数据采集（大）
     *
     * @param TerminalID 实时数据采集（大）主键
     * @return 实时数据采集（大）
     */
    @Override
    public Realtimegatherdata selectRealtimegatherdataByTerminalID(String TerminalID) {
        return realtimegatherdataMapper.selectRealtimegatherdataByTerminalID(TerminalID);
    }

    /**
     * 查询实时数据采集（大）列表
     *
     * @param realtimegatherdata 实时数据采集（大）
     * @return 实时数据采集（大）
     */
    @Override
    public List<Realtimegatherdata> selectRealtimegatherdataList(Realtimegatherdata realtimegatherdata) {
        return realtimegatherdataMapper.selectRealtimegatherdataList(realtimegatherdata);
    }

    @Override
    public List<Realtimegatherdata> getRealtimegatherdataList(String terminalId, String terminalName,String signPlatform,String[] locationNbs,Integer loginUserId,String sheetsCode,Integer systemFlag,String imei,String tenantId) {
        return realtimegatherdataMapper.getRealtimegatherdataList(terminalId, terminalName, signPlatform, locationNbs, loginUserId, sheetsCode, systemFlag,imei,tenantId);
    }

    @Override
    public List<Realtimegatherdata> getRealtimegatherdataListWX(String TerminalID, String TerminalName, String AreaCode, String startTime, String endTime) {
        return realtimegatherdataMapper.getRealtimegatherdataListWX(TerminalID, TerminalName, AreaCode, startTime, endTime);
    }

    /**
     * 新增实时数据采集（大）
     *
     * @param realtimegatherdata 实时数据采集（大）
     * @return 结果
     */
    @Override
    public int insertRealtimegatherdata(Realtimegatherdata realtimegatherdata) {
        return realtimegatherdataMapper.insertRealtimegatherdata(realtimegatherdata);
    }

    /**
     * 批量插入
     *
     * @param list
     * @return
     */
    @Override
    public void batchinsertRealtimegatherdata(List<Realtimegatherdata> list) {
        realtimegatherdataMapper.batchinsertRealtimegatherdata(list);
    }

    /**
     * 修改实时数据采集（大）
     *
     * @param realtimegatherdata 实时数据采集（大）
     * @return 结果
     */
    @Override
    public int updateRealtimegatherdata(Realtimegatherdata realtimegatherdata) {
        return realtimegatherdataMapper.updateRealtimegatherdata(realtimegatherdata);
    }

    /**
     * 批量删除实时数据采集（大）
     *
     * @param TerminalIDs 需要删除的实时数据采集（大）主键
     * @return 结果
     */
    @Override
    public int deleteRealtimegatherdataByTerminalIDs(String[] TerminalIDs) {
        return realtimegatherdataMapper.deleteRealtimegatherdataByTerminalIDs(TerminalIDs);
    }

    /**
     * 删除实时数据采集（大）信息
     *
     * @param TerminalID 实时数据采集（大）主键
     * @return 结果
     */
    @Override
    public int deleteRealtimegatherdataByTerminalID(String TerminalID) {
        return realtimegatherdataMapper.deleteRealtimegatherdataByTerminalID(TerminalID);
    }

    @Override
    public List<MapTerminalInfo> SearchTerminal(String terminalID, String terminalName, String areaid, String startTime, String endTime) {
        return realtimegatherdataMapper.SearchTerminal(terminalID, terminalName, areaid, startTime, endTime);
    }

    //更新实时数据表，add/update
    @Override
    public void synchronousMostValueFromRealtimegatherdata(TerminalHistoryData t, TerminalsDbProtocol terminalsDb){
            Realtimegatherdata realtimegatherdata = new Realtimegatherdata();
            realtimegatherdata.setLocationNb(terminalsDb.getLocationNb());
            List<Realtimegatherdata> isupate = selectRealtimegatherdataList(realtimegatherdata);
            realtimegatherdata.setReadNumber(t.getReadNumber());
            realtimegatherdata.setReverseNumber(t.getReverseNumber());
            realtimegatherdata.setInstantNumber(t.getInstantNumber());
            realtimegatherdata.setTotalFlow(t.getTotalFlow());
            realtimegatherdata.setPressure(t.getPressure());
            realtimegatherdata.setLocationNb(t.getLocationNb());
            realtimegatherdata.setTerminalId(t.getTerminalId());
            realtimegatherdata.setTenantId(t.getTenantId());
            realtimegatherdata.setTerminalVol(t.getTerminalVol());
            realtimegatherdata.setTerminalCsq(t.getTerminalCsq());
            realtimegatherdata.setTerminalCsqDescribe(t.getTerminalCsqDescribe());
            if(null != t.getValveState()){
                realtimegatherdata.setValveState(Math.toIntExact(t.getValveState()));
            }

            realtimegatherdata.setReadDate(t.getReadDate());
            if(isupate.size()>0){
                //更新
                updateRealtimegatherdata(realtimegatherdata);
            }else{
                //插入
                insertRealtimegatherdata(realtimegatherdata);
            }

    }
}
